var windowInstallPage;
var id_timeout;
function isPageInstalled(id_page, checkbox,check_box_obj)
{

	result = FB.api('/' + id_page, function(response) {
		if (response.has_added_app)
		{
			checkbox.checked = true;
		}
		else
		{
			if(confirm(confirm_add_fanpage))
			{
				windowInstallPage = window.open("http://www.facebook.com/add.php?api_key=" + social_app_id + "&pages=1&page=" + id_page, 
                '_blank',
                'width=650,height=320,resizable=no,scrollbars=no,toolbar=no,menubar=no,location=no,directories=no,status=no');
				id_timeout = setTimeout(function(){
					checkPageInstalled(id_page,checkbox,check_box_obj)
				},5000);
				checkbox.checked = false;
			}
			else
			{
				checkbox.checked = false; 
			}	
		}
	});
    
}
function checkPageInstalled(id_page, checkbox, check_box_obj)
{
	result = FB.api('/' + id_page, function(response) {
		if (response.has_added_app)
		{
            windowInstallPage.close();
            var image = " <img width='16' title='Already installed' src='" +base_url+ "/media/skin/classic/image/apply-icon.gif'/>";
			check_box_obj.parent().children('img').remove();
			check_box_obj.parent().append(image);
			checkbox.checked = true;
			var tabs_added  = {};
			tabs_added[id_page] = 1;
			$.get(base_url,{
				'tabs_added': tabs_added
			});
			alert(add_fanpage_successfully);
		}
		else
		{
			id_timeout = setTimeout(function(){
				checkPageInstalled(id_page,checkbox, check_box_obj)
			},3000);
			checkbox.checked = false;
		}
	});    
}
function postToWall(title, desc, href, image, caption)
{
    FB.ui(
        {
            method: 'feed',
            app_id: social_app_id,
            name: title,
            link: href+'?ref='+social_user_id,
            picture: image,
            caption: caption,
            description: desc
        },
        function(response) {
        	// nothing to do here
        }
    );
}
function authorizeBeforeShare(share_url, permission)
{
	FB.getLoginStatus(function(response) {
		FB.login(
			function(response) {
				if (response.authResponse) {
					FB.api('/me', function(response) {
						share(response.id, share_url);
					});
				} else {
					alert('You need to authorize the application before you can vote, share or invite friends.')
				}	
			}
			, {
				scope : permission || ''
			});
	});
}
function authorizeBeforeInvite(entry_title, url, permission)
{
	FB.getLoginStatus(function(response) {
		FB.login(
			function(response) {
				if (response.authResponse) {
					FB.api('/me', function(response) {
						inviteFriends(response.id, entry_title, url);
					});
				} else {
					alert('You need to authorize the application before you can vote, share or invite friends.')
				}	
			}
			, {
				scope : permission || ''
			});
	});
}
function authorizeBeforeVote(redirect_url, permission)
{
	FB.getLoginStatus(function(response) {
		FB.login(
			function(response) {
				if (response.authResponse) {
					FB.api('/me', function(response) {
						top.location = redirect_url;
					});
				} else {
					alert('You need to authorize the application before you can vote, share or invite friends.')
				}	
			}
			, {
				scope : permission || ''
			});
	});
}
function authorizeBeforeGotoSubmit(redirect_url, permission)
{
	authorizeBefore(redirect_url, 'You need to authorize the application before you can submit an entry', permission);
}
function authorizeBefore(redirect_url, msg, permission)
{
	FB.getLoginStatus(function(response) {
		FB.login(
			function(response) {
				if (response.authResponse) {
					FB.api('/me', function(response) {
						top.location = redirect_url;
					});
				} else {
					alert(msg)
				}	
			}
			, {
				scope : permission || ''
			});
	});
}
function authorizeManagePages(redirect_url)
{
	FB.getLoginStatus(function(response) {
		FB.login(
			function(response) {
				if (response.authResponse) {
					FB.api('/me', function(response) {
						top.location = redirect_url;
					});
				} else {
					alert('You need to authorize the manage page permission before you can load your Fan Pages.')
				}	
			}
			, {
				scope : 'manage_pages'
			});
	});
}
function share(share_url)
{
	window.open(
		'http://www.facebook.com/sharer/sharer.php?u=' + share_url,
		'popUpWindow',
		'width=650,height=320,resizable=no,scrollbars=no,toolbar=no,menubar=no,location=no,directories=no,status=no'
		);
	return false;
}

function inviteFriends(entry_title, url, id_user)
{
	FB.ui({
		method: 'send',
		name: entry_title,
		link: url + id_user,
		display:'popup'
	});
}

function canvasScrollTo(left,top)
{
	FB.Canvas.scrollTo(left,top);
}

function detectNewComment(url, data, callback)
{
    FB.Event.subscribe('comment.create', function(){
        $.post(
            url,
            data,
            callback
        );
    });
}

function sendAppRequests(message, data)
{
	if (data == undefined)
		FB.ui({
			method: 'apprequests',
			message: message
		});
	else
		FB.ui({
			method: 'apprequests',
			message: message,
			data: data
		});
}